package com.hikvision.hrms.controller;

import com.hikvision.hrms.model.Department;
import com.hikvision.hrms.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @author : yangliu449
 * @date :  2019/2/22
 */
@Controller
public class DepartmentController {

    @Autowired
    DepartmentService departmentService;
//    total 用于生成数据库的id
    int total;
    //    显示部门页面
    @RequestMapping("/departmentShow")
    @ResponseBody
    public ModelAndView departmentShow(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("page/department");
        return modelAndView;
    }

//    显示全部的数据
    @RequestMapping("/departmentData")
    @ResponseBody
    public List<Department> departmentData(){
        List<Department> list = departmentService.findDepartments();
        total = list.size() +1;
        return list;
    }

    @RequestMapping("/addDepartment")
    @ResponseBody
    public void addDepartment(@RequestParam(value = "id") String id, @RequestParam(value = "name") String name,@RequestParam(value = "description") String description){
        Department department = new Department();
        department.setId(total);
        department.setName(name);
        System.out.println(total);
        department.setDescription(description);
        departmentService.addDepartment(department);
    }

    @RequestMapping("/updateDepartment")
    @ResponseBody
    public  void  updateDepartment(@RequestParam(value = "id") Integer id, @RequestParam(value = "name") String name,@RequestParam(value = "description") String description){
        Department department = new Department();
        department.setId(id);
        department.setName(name);
        department.setDescription(description);
        departmentService.updateDepartment(department);
    }

    @RequestMapping("/deleteDepartment")
    @ResponseBody
    public void  deleteDepartment(@RequestParam(value = "id") Integer id, @RequestParam(value = "name") String name,@RequestParam(value = "description") String description){
        departmentService.deleteDepartment(id);
    }
}

